package com.tcm.ucenter.web.controller;

import com.base.core.exception.ServiceException;
import com.base.core.pager.PageBean;
import com.base.core.utils.R;
import com.tcm.order.domain.dto.OrderDTO;
import com.tcm.order.service.OrderService;
import com.tcm.ucenter.service.PointsDeductionService;
import io.swagger.annotations.ApiOperation;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.util.Map;

@RestController
@RequestMapping("/ucenter")
public class OrderController {

    @DubboReference(version = "0.0.1-alpha", check = false, timeout = 3000)
    private OrderService orderService;

    @DubboReference(version = "0.0.1-alpha", check = false, timeout = 3000)
    private PointsDeductionService pointsDeductionService;

    /**
     * 根据id获取单条数据
     * @param orderId
     * @return
     * @throws Exception
     */
    @ApiOperation(value = "通过userId获取订单")
    @GetMapping("/order/view")
    public R view(@RequestParam("orderId") Long orderId) throws Exception {
        return R.success(orderService.findDetailByOrderId(orderId));
    }

    /**
     * 根据id统计所有订单
     * @param userId
     * @return
     * @throws Exception
     */
    @ApiOperation(value = "通过userId获取订单")
    @GetMapping("/order/count")
    public R countByUserId(@RequestParam Long userId) throws Exception {
        return R.success(orderService.countOrderByUser(userId));
    }

    /**
     * 查询用户订单
     * @return
     * @throws Exception
     */
    @ApiOperation(value = "获得用户所有订单")
    @GetMapping("/order/list/page")
    public R listPageByUserId(@RequestParam Long userId,
                              @RequestParam(defaultValue = "1") Long page,
                              @RequestParam(defaultValue = "10") Long pagesize) {
        PageBean<OrderDTO> orderPageBean;
        try {
            orderPageBean = orderService.listPageOrderByUser(userId, page, pagesize);
        }catch (ServiceException e) {
            e.printStackTrace();
            return R.fail(e.getResult().getCode(), e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return R.fail(e.getMessage());
        }
        return R.success(orderPageBean);
    }

    /**
     * 根据userId统计所有订单
     * @param userId
     * @return
     * @throws Exception
     */
    @ApiOperation(value = "通过orderId获取订单")
    @GetMapping("/order/status/count")
    public R countByStatusUserId(@RequestParam Long userId,@RequestParam Byte status) throws Exception {
        return R.success(orderService.countOrderByUser(userId, status));
    }

    /**
     * 查询用户订单
     * @return
     * @throws Exception
     */
    @GetMapping("/order/list/page/status")
    public R listPageByUserStatus(@RequestParam Long userId,
                              @RequestParam Byte status,
                              @RequestParam(defaultValue = "1") Long page,
                              @RequestParam(defaultValue = "10") Long pagesize) {
        PageBean<OrderDTO> orderPageBean = null;
        try {
            orderPageBean = orderService.listPageOrderByUser(userId, status, page, pagesize);
        }catch (ServiceException e) {
            e.printStackTrace();
            return R.fail(e.getResult().getCode(), e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return R.fail(e.getMessage());
        }
        return R.success(orderPageBean);
    }

    @ApiOperation(value = "创建订单")
    @RequestMapping(value = "/order/create", method = RequestMethod.POST)
    public R createOrder(@RequestBody OrderDTO order) {
        Comparable<?> orderId;
        try {
            orderId = orderService.create(order);
        } catch (ServiceException e) {
            e.printStackTrace();
            return R.fail(e.getResult().getCode(), e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return R.fail(e.getMessage());
        }
        return R.success(orderId.toString());
    }

    @ApiOperation(value = "取消订单")
    @RequestMapping(value = "/order/cancel", method = RequestMethod.POST)
    public R cancelOrder(@RequestBody OrderDTO order) {
        int result = 0;
        try {
            result = orderService.cancel(order.getUserId(), order.getId());
        } catch (ServiceException e) {
            e.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return R.success(result);
    }

    @ApiOperation(value = "删除订单")
    @RequestMapping(value = "/order/delete", method = RequestMethod.GET)
    public R deleteOrder(String orderId) {
        int result = 0;
        try {
            result = orderService.delete(Long.valueOf(orderId));
        } catch (ServiceException e) {
            e.printStackTrace();
            return R.fail(e.getResult().getCode(), e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
        }
        return R.success(result);
    }

    /**
     * 确认收货
     *
     * @param orderId
     * @return
     */
    @GetMapping("/order/receipt")
    public R receipt(@RequestParam Long orderId) {
        try {
            orderService.confirmOrderStatus(orderId);
            return R.success();
        } catch (ServiceException e) {
            e.printStackTrace();
            return R.fail(e.getResult().getCode(), e.getMessage());
        } catch (Exception e) {
            return R.fail(e.getMessage());
        }
    }

     /**
     *  获得可用积分
     * @param userId
     * @param price
     * @return
     */
    @GetMapping("/order/usable/point")
    public R usablePoint(@RequestParam Long userId, @RequestParam BigDecimal price) {
        Map<String,Object> res;
        try {
            res = pointsDeductionService.usablePoint(userId,price);
        } catch (Exception e) {
            return R.fail(e.getMessage());
        }
        return R.success(res);
    }

    @GetMapping("/order/save/payment")
    public R savePayment(@RequestParam Long id, @RequestParam String payChannel) {
        try{
            orderService.savePayment(id, payChannel);
            return R.success();
        }catch (ServiceException e){
            return R.fail(e.getResult().getCode(), e.getMessage());
        }catch (Exception e){
            return R.fail(e.getMessage());
        }
    }

}
